//
//  PlaceholderView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/12.
//

import UIKit

class PlaceholderView: UIView {
    private let messageLabel = UILabel()
    var options:NoDataPlaceholderType =  .NoData
    private var button = UIButton(type: .custom)
    private var iconImage = UIImageView()
    init(_ type:NoDataPlaceholderType) {
        super.init(frame: .zero)
        self.options = type
        backgroundColor = BACKGROUND_COLOR
        addSubview(messageLabel)
        messageLabel.textColor = B999_TITLE_COLOR
        messageLabel.font = Font_Size_14
        messageLabel.snp.makeConstraints({
            $0.centerX.equalToSuperview()
            $0.bottom.equalTo(snp.centerY)
        })
        
       createView(type)
    }
    private func createView(_ type:NoDataPlaceholderType){
        messageLabel.text = type.message
        if type == .NotLogined {
            iconImage.removeFromSuperview()
            button = UIButton(type: .custom)
            button.contentEdgeInsets = UIEdgeInsets.init(top: 10, left: 30, bottom: 10, right: 30)
            addSubview(button)
            button.setTitle(type.title, for: .normal)
            button.titleLabel?.font = Font_Size_14
            button.setTitleColor(.white, for: .normal)
            button.backgroundColor = RED_COLOR
            button.layer.cornerRadius = 5
            button.snp.makeConstraints({
                $0.centerX.equalToSuperview()
                $0.top.equalTo(messageLabel.snp.bottom).offset(10)
            })
            button.addTarget(self, action: #selector(login), for: .touchUpInside)
        }else{
            button.removeFromSuperview()
            iconImage = UIImageView(image:  UIImage(named: type.imageName))
            addSubview(iconImage)
            iconImage.snp.makeConstraints({
                $0.centerX.equalToSuperview()
                $0.bottom.equalTo(messageLabel.snp.top).offset(-10)
            })
        }
    }
     
    @objc func login(){
        let vc = LoginViewController().setNavicationBar()!
        vc.modalPresentationStyle = .fullScreen
        self.fatherViewController()?.present(vc, animated: true) {

        }
    }
    func reloadView(type:NoDataPlaceholderType?=nil){
        if type != nil {
            self.createView(type!)
        }else{
            self.createView(self.options)
        }
    }
    required init?(coder: NSCoder) {
        fatalError()
    }
}
enum NoDataPlaceholderType:Equatable {
    case Normal(title:String,message:String)
    case NoData
    case NoContent
    case NotNetwork
    case NotLogined
}
extension NoDataPlaceholderType:PlaceholderOptions{
    var title: String{
        switch self {
        case .NotLogined:
            return "立即登录"
        default:
            return ""
        }
    }
    var message: String{
        switch self {
        case .NotLogined:
            return "登录后可以查看"
        case .NotNetwork:
            return "当前网络不可用，点击重试"
        case .NoContent:
            return "暂无内容"
        default:
            return "暂无数据"
        }
    }
    var imageName: String{
        switch self {
        case .NotNetwork:
            return "notnetwork"
        default:
            return "nodata"
        }
    }
}

protocol PlaceholderOptions {
    var title:String{ get }
    var message:String{ get }
    var imageName:String{get}
}
